package com.zsn.ai.controller;

import com.zsn.ai.config.MysqlChatMemory;
import com.zsn.ai.config.RedisChatMemoryRepository;
import com.zsn.ai.repository.ChatHistoryRepository;
import com.zsn.ai.vo.MessageVO;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: ai-demo
 * @ClassName ChatHistoryController
 * @description:
 * @author: zsn
 * @create: 2025-09-24 09:01
 * @Version 1.0
 **/
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/ai/history")
public class ChatHistoryController {

    private final ChatHistoryRepository chatHistoryRepository;

    private final MysqlChatMemory mysqlChatMemory;
    @GetMapping (value = "/{type}")
    public List<String> getChatIds(@PathVariable("type") String type){
        return chatHistoryRepository.getChatIds(type);
    }
    @GetMapping(value = "/{type}/{chatId}")
    public List<MessageVO> getChatHistory(@PathVariable("type") String type, @PathVariable("chatId") String chatId){
        List<Message> messages = mysqlChatMemory.get(chatId);
        if (messages == null){
            return List.of();
        }
        return messages.stream().map(MessageVO::new).toList();
    }
}